package com.tool_stdy.ConcurrencyModel.ProductConsumer;

import com.tool_stdy.ConcurrencyModel.WorkMastser.Task;

import java.util.Queue;
import java.util.concurrent.CountDownLatch;

/**
 * Created by yan_li on 2016/11/11.
 */
public class Consumer implements Runnable {

    private Queue<Task> workQueue;
    private CountDownLatch latc;

    public Consumer(Queue<Task> workQueue, CountDownLatch latch) {
        this.workQueue = workQueue;
        this.latc = latch;
    }

    public void doJob(Task task) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(task.getPrice());
    }

    @Override
    public void run() {
        while (true) {
            Task task = workQueue.poll();
            if (task == null) {
                latc.countDown();
                break;
            }
            doJob(task);
            latc.countDown();
        }
    }
}
